package doc

import (
	"blog/app/dto"
	"blog/manage_basic_api"
	"encoding/json"
	"fmt"
)

func main2() {

	// 这里json字符串最外层是一个数组,所以先需要解析数组数据
	var result []dto.GetRegionProjectsData

	err := json.Unmarshal([]byte(manage_basic_api.TmpJson), &result)
	if err != nil {
		return
	}

	//fmt.Println(a)
	respMap := make(map[string]manage_basic_api.RegionProjectsData)
	for _, item := range result {
		// 再对里面的json数据进行格式化处理
		fmt.Println(item.RegionId)
		// 格式化数据
		if _, ok := respMap[item.RegionId]; ok {
			fmt.Println("拿到了,要追加数据", item.RegionName)
			projectList := respMap[item.RegionId].ProjectList
			projectList = append(projectList, &manage_basic_api.ProjectsOfRegion{
				ProjectId:   item.ProjectId,
				ProjectName: item.ProjectName,
				Status:      item.Status,
			})
			respMap[item.RegionId] = manage_basic_api.RegionProjectsData{
				RegionName:  item.RegionName,
				RegionId:    item.RegionId,
				ProjectList: projectList,
			}
		} else {
			fmt.Println("还没有key,进行添加数据", item.RegionName)
			var projectList []*manage_basic_api.ProjectsOfRegion
			projectList = append(projectList, &manage_basic_api.ProjectsOfRegion{
				ProjectId:   item.ProjectId,
				ProjectName: item.ProjectName,
				Status:      item.Status,
			})
			respMap[item.RegionId] = manage_basic_api.RegionProjectsData{
				RegionName:  item.RegionName,
				RegionId:    item.RegionId,
				ProjectList: projectList,
			}
		}
	}

	fmt.Println("============= 格式化取出 value =================")
	for _, item := range respMap {
		fmt.Println(item)
	}

}
